package cn.chencaiju.blog.utils;

import java.io.IOException;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class SessionFactoryUtils {
	private static SqlSessionFactory factory;

	public static SqlSessionFactory getFactory() {
		if (factory == null) {
			// factory只初始化一次, 只在初始化的时候做线程同步
			// --- ① ② double check
			synchronized (SessionFactoryUtils.class) {
				if (factory == null) {
					try {
						factory = new SqlSessionFactoryBuilder()
								.build(Resources.getResourceAsStream("mybatisConfig.xml"));

					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}
		return factory;
	}	
}
